<template>
	<view>
		<view class="cu-list menu">
			<view class="cu-item">
				<view class="content">
					<text class="text-grey">当前小区</text>
				</view>
				<view class="action">
					<text class="text-grey text-sm">{{communityName}}</text>
				</view>
			</view>
		</view>
		<view v-if="gates.length > 0">
			<view class="cu-list menu">
				<view class="cu-item" v-for="(item, idx) in gates" :key="idx" @click="openDoor(item)">
					<view class="content padding-tb-sm">
						<view>
							<text class="cuIcon-clothesfill text-blue margin-right-xs"></text> {{item.locationObjName}}</view>
						<view class="text-gray text-sm">
							<text class="cuIcon-infofill margin-right-xs"></text> {{item.machineCode}}</view>
					</view>
				</view>
			</view>
		</view>
		<view v-else>
			<no-data-page></no-data-page>
		</view>
	</view>
</template>

<script>
	
	// pages/applicationKeyLocation/applicationKeyLocation.js
	const context = require("../../context/Java110Context.js");
	const constant = context.constant;
	const factory = context.factory;
	import noDataPage from '@/components/no-data-page/no-data-page.vue';
	const mqtt = require('mqtt/dist/mqtt')
	// #ifdef H5
	const client = mqtt.connect('ws://118.24.160.190:8083/mqtt');
	// #endif
	// #ifdef MP-WEIXIN||APP-PLUS
	const client = mqtt.connect('wx://118.24.160.190:8083/mqtt');
	// #endif
	export default {
		data() {
			return {
				gates: [],
				communityName: '',
				communityId: ''
			};
		},
		components: {
			noDataPage
		},
		props: {},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) { //this._loadOwnerLocation();
			context.onLoad(options);
			this.connect();
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			this.loadOwnerGates();
		},

		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {},

		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {},

		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {},

		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {},
		methods: {
			connect() {
				client.on('connect', function() {
					console.log('on connect')
					client.subscribe('RESPONSE/iFaceTerm/V1', function(err) {
						if (!err) {
							console.log('subscribe succes')
							// client.publish('presence', 'hello mqtt')
						}
					})
				}).on('reconnect', function() {
					console.log('on reconnect')
				}).on('error', function() {
					console.log('on error')
				}).on('end', function() {
					console.log('on end')
				}).on('message', function(topic, message) {
					console.log(message)
					console.log(message.toString())
				})
				// console.log('mqttMsg')
				// client.on('connect', (e) => {
				// 	console.log("连接成功！！！")
				// 	client.subscribe('RESPONSE/iFaceTerm/V1', {
				// 		qos: 0
				// 	}, (error) => {
				// 		if (!error) {
				// 			console.log('订阅成功')
				// 		} else {
				// 			console.log('订阅失败')
				// 		}
				// 	})

				// })
				// 接收消息处理
				// client.on('message', (topic, message) => {
				// 	console.log('收到来自', topic, '的消息', message.toString())
				// 	this.msg = message.toString()
				// })
				// client.on('error', function(error) {
				// 	console.log(error)
				// })
			},
			loadOwnerGates: function() {
				let _that = this;

				context.getOwner(function(_owner) {
					console.log('数据执行', _owner);
					let _data = {
						memberId: _owner.memberId,
						communityId: _owner.communityId,
						// machineType: '9999'
					};
					_that.communityName = _owner.communityName;
					_that.communityId = _owner.communityId;


					context.request({
						url: constant.url.listOwnerMachines,
						header: context.getHeaders(),
						method: "GET",
						data: _data,
						//动态数据
						success: function(res) {
							console.log('查询业主门禁', res); //成功情况下跳转

							if (res.statusCode == 200) {
								_that.gates = res.data.machines;
							}
						},
						fail: function(e) {
							wx.showToast({
								title: "服务器异常了",
								icon: 'none',
								duration: 2000
							});
						}
					});
				});
			},
			openDoor: function(item) {
				const topic =  'iFaceTerm/'+ item.machineCode + '/V1/DEVICE/OPEN_DOOR' 
				client.publish(topic, "hello")
			}
		}
	};
</script>
<style>
	
</style>
